package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.UserDto;
import com.zzyl.entity.User;
import com.zzyl.entity.UserRole;
import com.zzyl.mapper.UserMapper;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.service.UserService;
import com.zzyl.utils.NoProcessing;
import com.zzyl.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description UserServiceImpl
 * @Author loe
 * @Date 2024-07-15
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 分页查询
     *
     * @param userDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<UserVo> limitSearch(UserDto userDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        if (!userDto.getDeptNo().isEmpty()) {
            userDto.setDeptNo(NoProcessing.processString(userDto.getDeptNo()));
        }
        Page<UserVo> page = userMapper.limitSearch(userDto);
        return PageResponse.of(page, UserVo.class);
    }

    /**
     * 用户添加
     *
     * @param userDto
     */
    @Override
    @Transactional
    public void insertUser(UserDto userDto) {
        User user = BeanUtil.toBean(userDto, User.class);
        user.setDataState(SuperConstant.DATA_STATE_0);
        user.setPassword(SuperConstant.PASSWORD);
        // 新增自身的数据
        user.setUsername(userDto.getEmail());
        userMapper.insertSelective(user);
        // 新增用户角色关联表数据
        List<UserRole> userRoleList = new ArrayList<>();
        for (String roleVoId : userDto.getRoleVoIds()) {
            UserRole userRole = UserRole.builder()
                    .dataState(SuperConstant.DATA_STATE_0)
                    .userId(user.getId())
                    .roleId(Long.valueOf(roleVoId))
                    .build();
            userRoleList.add(userRole);
        }
        userRoleMapper.batchInsert(userRoleList);
    }

    /**
     * 用户修改
     * @param userDto
     */
    @Override
    @Transactional
    public void update(UserDto userDto) {
        // 更新用户信息表
        User user = BeanUtil.toBean(userDto, User.class);
        userMapper.updateByPrimaryKeySelective(user);

        // 更新用户角色关联表, 先删除, 再添加
        userRoleMapper.deleteByUserId(userDto.getId());
        // 添加
        List<UserRole> userRoleList = new ArrayList<>();
        for (String roleVoId : userDto.getRoleVoIds()) {
            UserRole userRole = UserRole.builder()
                    .userId(userDto.getId())
                    .roleId(Long.valueOf(roleVoId))
                    .dataState(SuperConstant.DATA_STATE_0)
                    .build();
            userRoleList.add(userRole);
        }
        userRoleMapper.batchInsert(userRoleList);
    }

    /**
     * 启用禁用
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, String status) {
        userMapper.updateStatus(id, status);
    }

    /**
     * 删除用户
     * @param userIds
     */
    @Override
    public void delete(String userIds) {
        Long id = Long.valueOf(userIds);
        userMapper.delete(id);
    }
}
